import _regeneratorRuntime from "@babel/runtime/regenerator";
import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
import _set from "lodash/set";
import _cloneDeep from "lodash/cloneDeep";
import { getCurrentOrganizationId, getResponse, resolveRequire } from "../utils/utils";
import { queryPromptLocale } from "./api";
/**
 * 获取 ui 库 的多语言, 如果传了 language 和 promptKey, 那么使用传递的 多语言来覆盖 ui库多语言
 * @param {function()} loadFileIntl - 获取文件多语言的方法: () => Promise<any>
 * @param {string} language - 语言
 * @param {string} promptKey - 多语言编码前缀
 * @return {Promise<*>}
 */

export function getFileIntl() {
  return _getFileIntl.apply(this, arguments);
}
/**
 * 获取antd的国际化
 * antd/lib/locale-provider/{language}.js
 * @param {String} language
 */

function _getFileIntl() {
  _getFileIntl = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
    var loadFileIntl,
        language,
        promptKey,
        organizationId,
        l,
        iRes,
        i,
        prefixLen,
        _args = arguments;
    return _regeneratorRuntime.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            loadFileIntl = _args.length > 0 && _args[0] !== undefined ? _args[0] : function () {};
            language = _args.length > 1 ? _args[1] : undefined;
            promptKey = _args.length > 2 ? _args[2] : undefined;
            organizationId = getCurrentOrganizationId();
            l = {};
            _context.prev = 5;
            _context.next = 8;
            return loadFileIntl();

          case 8:
            l = _context.sent;
            l = _cloneDeep(resolveRequire(l));
            _context.next = 15;
            break;

          case 12:
            _context.prev = 12;
            _context.t0 = _context["catch"](5);
            l = {};

          case 15:
            iRes = {};

            if (!(language && promptKey && organizationId !== undefined)) {
              _context.next = 23;
              break;
            }

            _context.next = 19;
            return queryPromptLocale(organizationId, language, promptKey);

          case 19:
            iRes = _context.sent;
            i = getResponse(iRes) || {};
            prefixLen = promptKey.length + 1; // 覆盖 ui库 多语言

            Object.keys(i).forEach(function (intlCode) {
              _set(l, intlCode.substr(prefixLen), i[intlCode]);
            });

          case 23:
            return _context.abrupt("return", l);

          case 24:
          case "end":
            return _context.stop();
        }
      }
    }, _callee, null, [[5, 12]]);
  }));
  return _getFileIntl.apply(this, arguments);
}

export function getCpaasUILocale(_x) {
  return _getCpaasUILocale.apply(this, arguments);
}
/**
 * 获取c7n的国际化
 * choerodon-ui/es/locale-provider/{language}.js
 * @param {String} language
 */

function _getCpaasUILocale() {
  _getCpaasUILocale = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(language) {
    return _regeneratorRuntime.wrap(function _callee2$(_context2) {
      while (1) {
        switch (_context2.prev = _context2.next) {
          case 0:
            return _context2.abrupt("return", getFileIntl(function () {
              return import("antd/es/locale-provider/".concat(language.replace('-', '_'), ".js"));
            }, language, 'cpaas.cpaasUI'));

          case 1:
          case "end":
            return _context2.stop();
        }
      }
    }, _callee2);
  }));
  return _getCpaasUILocale.apply(this, arguments);
}

export function getC7nLocale(_x2) {
  return _getC7nLocale.apply(this, arguments);
}
/**
 * 获取c7n-pro的国际化
 * choerodon-ui/pro/lib/locale-context/{language}.js
 * @param {String} language
 */

function _getC7nLocale() {
  _getC7nLocale = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee3(language) {
    return _regeneratorRuntime.wrap(function _callee3$(_context3) {
      while (1) {
        switch (_context3.prev = _context3.next) {
          case 0:
            return _context3.abrupt("return", getFileIntl(function () {
              return import("choerodon-ui/es/locale-provider/".concat(language.replace('-', '_'), ".js"));
            }, language, 'cpaas.c7nUI'));

          case 1:
          case "end":
            return _context3.stop();
        }
      }
    }, _callee3);
  }));
  return _getC7nLocale.apply(this, arguments);
}

export function getC7nProLocale(_x3) {
  return _getC7nProLocale.apply(this, arguments);
}

function _getC7nProLocale() {
  _getC7nProLocale = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee4(language) {
    return _regeneratorRuntime.wrap(function _callee4$(_context4) {
      while (1) {
        switch (_context4.prev = _context4.next) {
          case 0:
            return _context4.abrupt("return", getFileIntl(function () {
              return import("choerodon-ui/pro/lib/locale-context/".concat(language.replace('-', '_'), ".js"));
            }, language, 'cpaas.c7nProUI'));

          case 1:
          case "end":
            return _context4.stop();
        }
      }
    }, _callee4);
  }));
  return _getC7nProLocale.apply(this, arguments);
}